﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RuoTeng.Windows.Forms
{   
  
    public class RuoTengControl : System.Windows.Forms.UserControl
    {
        public int Sort = 1;

        protected  System.Windows.Forms.PictureBox _toolBar;

        public MenuBox MenuBox;

        public System.Windows.Forms.PictureBox ToolBar
        {
            get { return this._toolBar; }
            protected set { this._toolBar=value;}
        }


        /// <summary>
        /// 授权函数接口 每个控件对授权信息响应处理的内容不一样,
        /// 继承实现该接口就可以了
        /// </summary>
        /// <param name="sort"></param>
        public  virtual void Authorization(SortedList<string, object> sort)
        {
 
        }

        protected  virtual void pictureBox_Click(object sender, EventArgs e)
        {
            this.ShowPanel();
        }

        /// <summary>
        /// 将当前模块变为可视化模块。
        /// </summary>
        public void ShowPanel()
        {
            if (this.MenuBox != null)
            {
                foreach (KeyValuePair<string, RuoTengControl> keyValue in this.MenuBox.ToolControls)
                {
                    keyValue.Value.Visible = keyValue.Value.Equals(this) ? true : false;
                }
            }
        }

        /// <summary>
        /// 创建模块控件的相应的菜单控件
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="name"></param>
        protected virtual void CreatePictureBox(System.Drawing.Bitmap bitmap,string name)
        {
            this.ToolBar = new System.Windows.Forms.PictureBox();
            this.ToolBar.Image = bitmap;
         //注册单击事件,用于切换当前模块
            this.ToolBar.Click+= new System.EventHandler(this.pictureBox_Click);
        }


        /// <summary>
        /// 获取SortedList中元素
        /// </summary>
        /// <param name="name">键值</param>
        /// <param name="defaultVaule">不存在默认值</param>
        /// <param name="sort">集合对象</param>
        /// <returns></returns>
        protected  bool GetValue(string name,bool defaultVaule,SortedList<string,object> sort)
        {
             if(sort.ContainsKey(name))
             {
                 object o=sort[name];
                 if(o is bool)
                 {
                    return (bool)o;
                 }
                 return defaultVaule;
             }
             else{
                 return defaultVaule;
             }
        }
        /// <summary>
        /// 获取SortedList中元素
        /// </summary>
        /// <param name="name">键值</param>
        /// <param name="defaultVaule">不存在默认值</param>
        /// <param name="sort">集合对象</param>
        /// <returns></returns>
        protected string GetValue(string name, string defaultVaule, SortedList<string, object> sort)
        {
            if (sort.ContainsKey(name))
            {
                object o = sort[name];
                if (o is string)
                {
                    return o.ToString();
                }
                return defaultVaule;
            }
            else
            {
                return defaultVaule;
            }
        }
    }
}
